<template>
  <div>
    <form>
      <el-card>
        <!--上方搜索栏-->
        <div class="demo-input-size">
        </div>



        <!--表格区-->
        <el-table :data="queryList" border stripe>
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column label="设备名" prop="name"></el-table-column>
          <el-table-column label="总数" prop="total"></el-table-column>
          <el-table-column label="可租赁数" prop="remain"></el-table-column>
          <el-table-column label="租赁花费(元/每小时)" prop="rentCost"></el-table-column>
          <el-table-column label="租赁数目">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.count"
                placeholder="请输入您要租赁的数目">
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" round size="mini" @click="rent(scope.row)">租赁</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--  分页区-->

      </el-card>
    </form>
  </div>
</template>

<script>


export default {
  name: "UserEq",
  mounted() {
    this.query();
  },
  data() {
    return {
      queryInfo: {},
      queryList: [],
      pageSize: 7,
    }
  },
  methods: {
    query() {
      this.$http.get("/admin/eq", {}).then(res => {
        if (res.data.code === 200) {
          this.queryList = res.data.data;
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(err => {
        this.$message.error("数据加载失败")
      })
    },
    rent(row) {
      let regexp = /^[1-9]\d*$/;
      if (!regexp.test(row.count) || row.count > row.remain) {
        this.$message.error("请输入有效的数字")
        return
      }
      this.$http.post("/user/rent", {
        no: row.no,
        count: parseInt(row.count),
        rentCost: row.rentCost,
      }).then(res => {
        if (res.data.code === 200) {
          this.$message.success("租赁成功");
          row.remain -= row.count;
          return;
        }
        this.$message.error(res.data.msg);
      }).catch(err => {
        console.log(err);
      })


    }


  }
}
</script>

<style scoped>

</style>
